﻿Imports System.Text.RegularExpressions
Imports Sy.Data
''' <summary>
''' 全局模块
''' </summary>
Public Module MGlobal
    Public PhoneReg As New Regex("1[345678]\d{9}")
    Public Log As Action(Of String)
    ''' <summary>
    ''' 添加一条信息记录
    ''' </summary>
    Public UpdateViewOne As Action(Of CProductInfo)
    ''' <summary>
    ''' 传入的主窗体
    ''' </summary>
    Friend _Frm_Main As Frm_Main
    ''' <summary>
    ''' 数据库对象
    ''' </summary>
    ''' <returns></returns>
    Friend Property DB As Abs_DB
    ''' <summary>
    ''' 初始化工作只进行一次的标识符
    ''' </summary>
    Public Inited As Boolean = False
    ''' <summary>
    ''' 采集超过PageCount条将不在显示更新
    ''' </summary>
    Public PageCount As Integer = 15
#Region "初始化工作"
    Public Sub InitGlobalVariables(Frm As Frm_Main)
        _Frm_Main = Frm
        Dim rb() As AreasResponse = LoadAreaTree()
#Region "Log日志"
        Log = Sub(Str As String)
                  _Frm_Main.TxtLog.AppendLogOnThread($"{vbNewLine}{Str}")
              End Sub
#End Region
#Region "创建数据库"

#End Region
#Region "配置全局唯一的map配置"
        C_Config_Map.Init()
#End Region
        With Frm
            .List_Keywords.Items.AddRange(CKeyword.Load)
            .List_Area.Items.AddRange(CAreaInfo.Load)
            .List_Filter_Keywords.Items.AddRange(CFilterKeyword.Load)
            .CmbCity.Visible = False
            .CmbCountry.Visible = False
            .CmbProvince.Items.Clear()
            '从资源文件加载省市区
            .CmbProvince.Items.AddRange(rb)
#Region "加载地图类型"
            Dim items = Sy.Enum.GetItems(Function(i As Sy.Enum.Item(Of MapMode)) Not i.Value = MapMode.Unknow AndAlso Not i.Value = MapMode.GaoDe)
            .CkListbox_FromMap.Items.AddRange(items)
#End Region
#Region "初始化地图选中项，记忆关闭之前最后一次操作的设置,you wen ti "
            For i = C_Config_Map.Instance.ChecksDic.Keys.Count - 1 To 0 Step -1
                Dim it = C_Config_Map.Instance.ChecksDic.Keys(i)
                '如果不包含it这个字符串，那么值为fasle，取not ,就会是true,刚好可以删除这个不包含的元素
                If (Not CBool(items.Sum(Function(ie As Sy.Enum.Item(Of MapMode)) If(ie.GetDescription.Equals(it), 1, 0)))) Then C_Config_Map.Instance.ChecksDic.Remove(it)
            Next
            C_Config_Map.Instance.Edt()
            For i = 0 To .CkListbox_FromMap.Items.Count - 1
                Dim o As Sy.Enum.Item(Of MapMode) = .CkListbox_FromMap.Items(i)
                '讲checksDic字典中的值赋值给控件checksDic
                If C_Config_Map.Instance.ChecksDic.ContainsKey(o.GetDescription) Then
                    .CkListbox_FromMap.SetItemChecked(i, C_Config_Map.Instance.ChecksDic(o.GetDescription))
                End If
            Next
#End Region
            'listview第一页显示
            .Pager1.DoAdpterUI()
        End With
#Region "定义窗体更新操作回调"
        UpdateViewOne = Sub(Item As CProductInfo)
                            With _Frm_Main
                                .BeginInvoke(Sub()
                                                 If _Frm_Main.LstV_Task.Items.Count < PageCount Then
                                                     Dim itm As ListViewItem
                                                     Dim I = .LstV_Task.Items.Count + 1
                                                     .LstV_Task.SuspendLayout()
                                                     itm = .LstV_Task.Items.Add(Sy.UI.Common.UIAdapter.GetListViewItem(Item, I))
                                                     itm.Tag = Item
                                                     .LstV_Task.ResumeLayout()
                                                 End If
                                                 '.Pager1.TotalCount += 1
                                             End Sub)
                            End With

                        End Sub
#End Region
        Inited = True
    End Sub
#End Region
End Module

